package com.th.cash.th2.product

import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.th.basemodel.base.BaseViewModel
import com.th.basemodel.ext.bindData
import com.th.basemodel.ext.divider
import com.th.basemodel.ext.dp2px
import com.th.basemodel.ext.vertical
import com.th.resmodel.R
import com.th.resmodel.databinding.FragmentDetailBinding
import kotlinx.coroutines.launch

class DetailFragment : VerifyBaseFragment<FragmentDetailBinding>() {

    val mList = mutableListOf<ProductDetailResponse.IRkkiSSThoroughnessTHZFGop>()

    private val mViewModel by viewModels<ProductVm>()

    private var nextStep: String = ""

    override var type = 0

    override fun startObserve() {
        lifecycleScope.launch {
            mViewModel.uiState.collect {
                when (it) {
                    is ProductState.ProductDetailData -> {
                        setOid(it.data.QtlLPQXSonataCCkojtD?.pcVvsZyTotipalmationJWiscBh ?: "")

                        nextStep = it.data.EuPVlTORenvoiZHVUeJY?.noneconomicSingle ?: ""

                        mList.clear()
                        if (it.data.iRkkiSSThoroughnessTHZFGop?.isNotEmpty() == true) {
                            mList.addAll(it.data.iRkkiSSThoroughnessTHZFGop!!.toMutableList())
                        }

                        mBinding.rvProductInfoList.adapter?.notifyDataSetChanged()
                    }

                    else -> {}
                }
            }
        }
    }

    override fun init() {
        initAdapter()
        initClick()

        mViewModel.intentChannel.trySend(ProductIntent.GetProductDetail(ProductDetailRequest(getPid())))
    }

    private fun initClick() {
        mBinding.header.headBack.setOnClickListener {
            back(0)
        }

        mBinding.tvProductInfoSubmit.setOnClickListener {
            nextPage(nextStep, false)
        }
    }

    private fun initAdapter() {
        mBinding.rvProductInfoList.vertical(2)
            .divider(
                color = ContextCompat.getColor(requireContext(), android.R.color.transparent),
                size = dp2px(10f)
            )
            .bindData(mList, R.layout.item_product_verify) { holder, t, position ->
                Glide.with(holder.itemView.context)
                    .load(t.sdEJmFEEquivoqueKLvgNiu)
                    .into(holder.getView(R.id.ivProductVerify))

                holder.itemView.setOnClickListener {
                    if (t.PqEnbyFGeobiologicalUCbiZNe == 1) {
                        nextPage(t.bubbleheadSingle)
                    } else {
                        nextPage(nextStep)
                    }
                }
            }
    }

    override fun onLazyInitData() {

    }

    override fun getVm(): BaseViewModel {
        return mViewModel
    }

    companion object {
        fun newInstance(): DetailFragment {
            return DetailFragment()
        }
    }

}